Skip to content

compose: increase zram at runtime#276

Closed
jakogut wants to merge 1 commit intomasterfrom
zram
Closed

compose: increase zram at runtime#276
jakogut wants to merge 1 commit intomasterfrom
zram

Conversation

@jakogut
Copy link

@jakogut jakogut commented Oct 28, 2024

To alleviate memory pressure, increase zram to 50% of total memory at runtime. This allows the host to compress inactive pages to reduce contention.

This service optionally allows for the maximum zram percentage and compression algorithm to be modified at runtime using variables.

Change-type: patch

@klutchell
Copy link
Contributor

I haven't seen any OOM on this device recently, which is promising. We can leave it for longer and see how it goes.
https://dashboard.balena-cloud.com/devices/ace0fda69b68c9d6085ebeb11866baf6

@ab77 ab77 removed their request for review October 31, 2024 22:01
To alleviate memory pressure, increase zram to 50% of total memory at
runtime. This allows the host to compress inactive pages to reduce
contention.

This service optionally allows for the maximum zram percentage and
compression algorithm to be modified at runtime using variables.

Change-type: patch
Signed-off-by: Joseph Kogut <joseph@balena.io>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a new zram service to the Docker Compose configuration to dynamically increase zram (compressed swap) capacity at runtime to alleviate memory pressure on the host system.

  • Introduces a new service that configures zram to use 50% of total memory by default
  • Implements host filesystem access through a privileged chroot container
  • Provides configurable parameters for zram percentage and compression algorithm

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

command:
- |
set -e
[[ $ENABLED == 'true' ]] || exit
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using bash-specific syntax [[ ]] in a shell script that starts with /bin/sh. Use POSIX-compliant [ ] instead: [ "$ENABLED" = 'true' ] || exit

Suggested change
[[ $ENABLED == 'true' ]] || exit
[ "$ENABLED" = 'true' ] || exit

Copilot uses AI. Check for mistakes.
trap stop_container EXIT

host_cmd() {
if [ -z "$(docker ps -qf "NAME=$HOST_CONTAINER_NAME")" ]; then
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docker filter syntax is incorrect. The NAME filter should use name instead of NAME: docker ps -qf "name=$HOST_CONTAINER_NAME"

Suggested change
if [ -z "$(docker ps -qf "NAME=$HOST_CONTAINER_NAME")" ]; then
stop_container() { docker stop "$(docker ps -qf "name=$HOST_CONTAINER_NAME")"; }
trap stop_container EXIT
host_cmd() {
if [ -z "$(docker ps -qf "name=$HOST_CONTAINER_NAME")" ]; then

Copilot uses AI. Check for mistakes.
trap stop_container EXIT

host_cmd() {
if [ -z "$(docker ps -qf "NAME=$HOST_CONTAINER_NAME")" ]; then
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docker filter syntax is incorrect. The NAME filter should use name instead of NAME: docker ps -qf "name=$HOST_CONTAINER_NAME"

Suggested change
if [ -z "$(docker ps -qf "NAME=$HOST_CONTAINER_NAME")" ]; then
stop_container() { docker stop "$(docker ps -qf "name=$HOST_CONTAINER_NAME")"; }
trap stop_container EXIT
host_cmd() {
if [ -z "$(docker ps -qf "name=$HOST_CONTAINER_NAME")" ]; then

Copilot uses AI. Check for mistakes.
alpine:3.20.3
fi

HOST_CONTAINER_ID="$(docker ps -qf "NAME=$HOST_CONTAINER_NAME")"
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docker filter syntax is incorrect. The NAME filter should use name instead of NAME: docker ps -qf "name=$HOST_CONTAINER_NAME"

Suggested change
HOST_CONTAINER_ID="$(docker ps -qf "NAME=$HOST_CONTAINER_NAME")"
HOST_CONTAINER_ID="$(docker ps -qf "name=$HOST_CONTAINER_NAME")"

Copilot uses AI. Check for mistakes.

host_cmd "swaps=\$(awk 'NR>1 { print \$1 }' /proc/swaps); if [ -n \"\$swaps\" ]; then swapoff \$swaps; fi"
host_cmd "zrams=\$(find /dev -name \"zram*\"); if [ -n \"\$zrams\" ]; then for d in \$zrams; do zramctl -r \$d; done; fi"
zram_dev=$(host_cmd "memtotal=\$(grep MemTotal /proc/meminfo | awk '{ print \$2 }'); zramctl --find --size \$((memtotal * $ZRAM_PCT / 100))K --algorithm $ZRAM_ALGO")
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variables $ZRAM_PCT and $ZRAM_ALGO are not validated before use in shell commands, which could lead to command injection. Add validation to ensure they contain only expected values.

Copilot uses AI. Check for mistakes.
if [ -z "$(docker ps -qf "NAME=$HOST_CONTAINER_NAME")" ]; then
docker run \
--interactive \
--tty=true \
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The --tty=true flag is unnecessary for a detached container that will only receive exec commands. Remove this flag to simplify the configuration.

Suggested change
--tty=true \

Copilot uses AI. Check for mistakes.
@klutchell
Copy link
Contributor

@jakogut couldn't this be simplified to avoid the host_cmd container wrapper by using the io.balena.features.procfs label?
https://docs.balena.io/reference/supervisor/docker-compose/#labels

It looks like we need /proc and /dev from the host to perform the operation, so with that label and privileged we should be able to accomplish the same thing without the additional container?

@jakogut
Copy link
Author

jakogut commented Aug 22, 2025

@klutchell That may work, though you may run into some other issue, it would have to be tested.

Swapon should work as long as it's run privileged (which it is) or has CAP_SYS_ADMIN.

@klutchell
Copy link
Contributor

Superseded by #672

@klutchell klutchell closed this Aug 22, 2025
auto-merge was automatically disabled August 22, 2025 18:41

Pull request was closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants